from os import error
from re import search
from flask import Blueprint,render_template, request,session,redirect
from config import conn,cur,mutex ,r as redis
import datetime

Search = Blueprint('Search',__name__)
 
@Search.route('/',methods=["GET", "POST"])
def index():
    return render_template('/search/index.html')
@Search.route('/detail',methods=["GET", "POST"])
def detail():
    return render_template('/search/detail.html')

@Search.route('/queryInfo',methods=["GET", "POST"])
def queryInfo():
    data =[]
    if request.method == "POST":
        patient_name = request.form.get("patient_name")
        doctor_name = request.form.get("doctor_name")
    elif request.method == "get":
        patient_name = request.form.get("patient_name")
        doctor_name = request.form.get("doctor_name")
    if (str(patient_name) =="None" or  str(patient_name)=="")and (str(doctor_name)=="None" or str(doctor_name)==""):
       cur.execute('select  * from personal_info')
    elif (str(patient_name)!="None" or  str(patient_name)!="") and (str(doctor_name)=="None" or str(doctor_name)==""):
       cur.execute("select  * from personal_info where name='"+patient_name+"'")
    elif (str(patient_name)=="None" or  str(patient_name)=="") and (str(doctor_name)!="None" or str(doctor_name)!=""):
       cur.execute("select  * from personal_info where doctor_id="+ str(doctor_name))
    else:
        print("aaaa")
        cur.execute("select  * from personal_info where name='"+patient_name+"'"+"and doctor_id = "+doctor_name) 
    row = cur.fetchone()
    while row:
        #print(row[3] == 1)
        data.append(row)
        row = cur.fetchone()
    return {'message':"success!",'dbdata':data}



@Search.route('/queryDetail',methods=["GET", "POST"])
def queryDetail():
    data =[]
    if request.method == "POST":
        id = request.form.get("id")
    elif request.method == "get":
        id = request.form.get("id")
    cur.execute("select  * from vital_signs where person_id="+ str(id))
    row = cur.fetchone()
    while row:
        #print(row[3] == 1)
        data.append(row)
        row = cur.fetchone()
    return {'message':"success!",'dbdata':data}

@Search.before_request
def before():
    if session.get('id') is None or session.get('role') != 'patient':
        return redirect('/Login/',code=301)
    else:
        pass